﻿using MediatR;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using S.Authorization.Common.Dto;
using S.Authorization.Domain.Model.Authority;
using S.Authorization.Domain.Model.User;
using S.Authorization.Infrastructure.Interface.User;
using S.Authorization.Read.API.Application.Command.UserCommand;
using S.Authorization.Read.API.Dto;
using S.Authorization.Read.API.Extensions;
using System.Security.Claims;

namespace S.Authorization.Read.API.Controllers
{
    public class BaseController: ControllerBase
    {
        public BaseController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        public IMediator mediator { get; }

        /// <summary>
        /// 用户ID
        /// </summary>
        protected virtual string? UserId => HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
        /// <summary>
        /// 角色
        /// </summary>
        protected virtual string? RoleName => HttpContext.User.FindFirst(ClaimTypes.Role)?.Value;
        /// <summary>
        /// 
        /// </summary>
        protected virtual UserStrategyDto? UserStrategyDto =>   GetUserLoginById(UserId.ToInt());

        protected UserStrategyDto GetUserLoginById(int userId)
        {
            return  mediator.Send(new UserGetByIdCommand()
            {
                UserId = userId,          
            }).Result;
        }
    }
}
